<?php
namespace Home\Model;
use \Frame\Libs\BaseModel;

final class DonateModel extends BaseModel{
	
	//获取某用户捐款记录数
	public function getMoneyRowsByUser($uid){
		$sql = "SELECT donate_money_id FROM love_donate_money WHERE uid='$uid'";
		return $this->pdo->rowCount($sql);
	}
	
	//获取某用户捐书记录数
	public function getBookRowsByUser($uid){
		$sql = "SELECT donate_book_id FROM love_donate_book WHERE uid='$uid'";
		return $this->pdo->rowCount($sql);
	}
	
	//分页获取某用户的捐款记录
	public function getUserDonateMoneyList($startrows,$pagesize,$uid){
//		$sql = "SELECT trade_no,money,donate_money_time,status,type FROM (SELECT uid,trade_no,money,donate_money_time,status,type FROM love_donate_money ORDER BY donate_money_id LIMIT $startrows,$pagesize) AS res WHERE uid=$uid";
		$sql = "SELECT trade_no,money,donate_money_time,status,type FROM (SELECT * FROM love_donate_money WHERE uid=$uid) AS res ORDER BY donate_money_id DESC LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	//分页获取某用户的捐书记录
	public function getUserDonateBookList($startrows,$pagesize,$uid){
//		$sql = "SELECT donate_book_id,donate_book_time,status FROM (SELECT * FROM love_donate_book ORDER BY donate_book_id LIMIT $startrows,$pagesize) AS res WHERE uid=$uid";
		$sql = "SELECT donate_book_id,donate_book_time,status FROM (SELECT * FROM love_donate_book WHERE uid=$uid) AS res ORDER BY donate_book_id DESC LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	//获取某用户某次捐书记录的详情数据
	public function getDonateBookDetail($id){
		$sql = "SELECT book_name,book_count,isbn,status FROM love_donate_book_detail WHERE donate_book_id='$id'";
		return $this->pdo->fetchAll($sql);
	}
	
	//插入捐书基本信息，返回插入的记录的ID
	public function addDonateBook($uid,$nickname,$contact){
		$sql = "INSERT INTO love_donate_book (uid,donate_book_time,contact,nickname) VALUES ($uid,now(),'$contact','$nickname')";
		$row = $this->pdo->exec($sql);
		if($row==1){
			return $this->pdo->lastInsertId();
		}else{
			return 0;
		}
	}
	
	//插入详细书籍信息
	public function addDonateBookDetail($donate_book_id,$book_name,$book_count,$isbn){
		$sql = "INSERT INTO love_donate_book_detail (donate_book_id,book_name,book_count,isbn) VALUES ($donate_book_id,'$book_name',$book_count,'$isbn')";
		$row = $this->pdo->exec($sql);
		if($row==1){
			$id = $this->pdo->lastInsertId();
			$arr = [
				'row' => $row,
				'id' => $id
			];
			return $arr;
		}else{
			$arr = [
				'row' => $row
			];
			return $arr;
		}
	}
	
	//删除某条捐书书籍详情
	public function delBookDetail($id){
		$sql = "DELETE FROM love_donate_book_detail WHERE donate_book_detail_id=$id";
		return $this->pdo->exec($sql);
	}
	
	//再捐款表新增一条记录，默认为未支付
	public function addNewPay($uid,$out_trade_no,$money,$type){
        $sql = "INSERT INTO love_donate_money (uid,trade_no,money,donate_money_time,type) VALUES ('$uid','$out_trade_no','$money',now(),'$type')";
        return $this->pdo->exec($sql);
    }
	
	//查出某条捐书明细的状态
	public function getStatus($id){
		$sql = "SELECT status FROM love_donate_book_detail WHERE donate_book_detail_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
}
?>